<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/26
 * Time: 9:22
 */

namespace app\index\controller;


use think\Controller;
use think\Db;

class Tool extends Controller
{

    /**
     * 文件上传
     * @return \think\response\Json
     */
    public function upload()
    {
        // 获取表单上传文件
        $file = request()->file('file');
        if (empty($file)) {
            return json([
                "code" => 1001,
                "data" => 'falied'
            ]);
        }

        /** @var \think\File $file */
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size' => 5242880, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $file_name = $info->getSaveName();
            $file_name = str_replace('\\', '/', $file_name);
            $image = "/uploads/" . $file_name;
            $image = reduce_image($image);
            return json([
                "code" => 0,
                "data" => $image
            ]);
        } else {
            return json([
                "code" => 1000,
                "data" => 'falied'
            ]);
        }
    }
    /**
     * layui 文件上传
     * @return \think\response\Json
     */
    public function layui_upload()
    {
        $file = request()->file('file');
        if (empty($file)) {
            return json([
                "code" => 1002,
                "msg" => "失败",
                "data" =>"失败"
            ]);
        }
        /** @var \think\File $file */
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size' => 5242880, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $file_name = $info->getSaveName();
            $file_name = str_replace('\\', '/', $file_name);
            $image = "/uploads/" . $file_name;
            $image = reduce_image($image);
            return json([
                "code" => 0,
                "msg" => "上传成功",
                "data" => [
                    "src" => $image,
                    "title" => "图片"
                ]
            ]);
        } else {
            return json([
                "code" => 1002,
                "msg" => "失败",
                "data" =>"失败"
            ]);
        }
    }
    /**
     * 接口文档
     * @param string $domain
     * @return array|mixed|\think\response\View
     */
    public function api_doc($domain = "")
    {
        if (empty($domain)) {
            $domain = $_SERVER["HTTP_HOST"];
        }
        /**
         * 预处理url
         * @param $s
         * @return mixed
         */
        $pre_hand_str = function ($s) {
            $s = str_replace("AppMap", "app_map", $s);
            $s = str_replace("AppV2", "app_v2", $s);
            return $s;
        };
        $config = [
            "\\app\\index\\controller\\Tool",
            "\\app\\index\\controller\\User",
            "\\app\\api\\controller\\V0",
            "\\app\\api\\controller\\V1",
            "\\app\\api\\controller\\V2",
            "\\app\\api\\controller\\V3",
            "\\app\\api\\controller\\V4",
            "\\app\\index\\controller\\Pay",
        ];
        $r_data = [];
        if (!is_array($config)) {
            exit("config error");
        }
        foreach ($config as $k => $class) {
            $ref = new \ReflectionClass($class);
            $methods = $ref->getMethods();
            if (!is_array($methods)) {
                continue;
            }
            foreach ($methods as $method) {
                $class = $method->class;
                $action = $method->name;
                $param = $method->getParameters();
                $doc = $method->getDocComment();
                if (strpos($class, "controller") != false) {
                    $c = str_replace("\\", "/", $class);
                    $c = str_replace("/controller/", "/", $c);
                    $c = str_replace("app/", "", $c);
                    $i_data["url"] = $pre_hand_str(
                        sprintf("%s/%s/%s", $domain, $c, $action)
                    );
                    $i_data["param"] = $param;
                    $i_data["doc"] = str_replace("\n", "<br/>", $doc);
                    $i_data["action"] = $class . "->" . $action . "()";
                    $i_data["class"] = $class;
                    $r_data[] = $i_data;
                }
            }
        }
        $this->assign("r_data", $r_data);
        return view();
    }


    /**
     * 接口文档
     * @param string $domain
     * @return array|mixed|\think\response\View
     */
    public function addons_doc($domain = "")
    {
        if (empty($domain)) {
            $domain = $_SERVER["HTTP_HOST"];
        }
        /**
         * 预处理url
         * @param $s
         * @return mixed
         */
        $pre_hand_str = function ($s) {
            $s = str_replace("AppMap", "app_map", $s);
            $s = str_replace("AppV2", "app_v2", $s);
            return $s;
        };
        $config = [
            "\\addons\\seller\\controller\\App",
            "\\addons\\user\\controller\\App"
        ];
        $r_data = [];
        if (!is_array($config)) {
            exit("config error");
        }
        foreach ($config as $k => $class) {
            $ref = new \ReflectionClass($class);
            $methods = $ref->getMethods();
            if (!is_array($methods)) {
                continue;
            }
            foreach ($methods as $method) {
                $class = $method->class;
                $action = $method->name;
                $param = $method->getParameters();
                $doc = $method->getDocComment();
                if (strpos($class, "controller") != false) {
                    $c = str_replace("\\", "", $class);
                    $c = str_replace("controller", "-", $c);
                    $c = str_replace("addons", "", $c);
                    $i_data["url"] = $pre_hand_str(
                        sprintf("%s/addons/execute/%s-%s", $domain, $c, $action)
                    );
                    $i_data["param"] = '';
                    $i_data["doc"] = str_replace("\n", "<br/>", $doc);
                    $i_data["action"] = $class . "->" . $action . "()";
                    $i_data["class"] = $class;
                    $r_data[] = $i_data;
                }
            }
        }
        $this->assign("r_data", $r_data);
        return view();
    }


    /**
     * 城市获取
     * @param int $pid
     * @return \think\response\Json
     */
    public function city($pid = 0)
    {
        if ($pid) {
            $data = Db::table("shop_city")->where(["city_parent_id" => $pid])->select();
        } else {
            $data = Db::table("shop_city")->where(["city_deep" => 1])->select();
        }
        return json([
            "code" => 0,
            "data" => $data,
        ]);
    }
}